const fs = require("fs");
const os = require("os");

const isChinese = (content) => {
    rexJp = /^[\u3040-\u309F\u30A0-\u30FF]+$/;
    rexChn = /^[\u4e00-\u9fa5]+$/;
    // if (rexJp.exec(content)) {
    //     console.log("Japanese");
    //     return true;
    // }else if (rexChn.exec(content)) {
    //     console.log("Chinese");
    //     return true;
    // }else {
    //     console.log("English");
    //     return false;
    // }
    var regChn = new RegExp("[\\u4E00-\\u9FFF]+","g");
    var regJp = new RegExp("[\\u3040-\\u309F\\u30A0-\\u30FF]+","g");
    if(regChn.test(content)){
        console.log("Chinese");
        return true;
    }else if(regJp.test(content)){
        console.log("Japanese");
        return true;
    }else {
        console.log("English");
        return false;
    }
}

const splitChn = (arr) => {
    console.log("split lyrics");
    var fWrite = fs.createWriteStream("output.txt");
    for(item of arr) {
        for(let i = 0; i < item.length; i++) {
            fWrite.write(item[i] + os.EOL);
        }
        fWrite.write("" + os.EOL);
    }

    fWrite.close();
}

const splitEn = (arr) => {
    console.log("split English lyrics");
    var fWrite = fs.createWriteStream("output.txt");
    for(item of arr) {
        let w = item.split(" ");
        for(let i = 0; i < w.length; i++) {
            fWrite.write(w[i] + os.EOL);
        }
        fWrite.write("" + os.EOL);
    }

    fWrite.close();
}

module.exports = {
    isChinese: isChinese,
    splitChn: splitChn,
    splitEn: splitEn
}